package modules.majiang.viewManager
{
	/**
	 *
	 * @author xumin.xu 2013-1-21
	 */

	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import modules.ViewManager;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablePlayerDataModel;
	import modules.majiang.data.TablesDataManager;

	public class DealerViewManager extends ViewManager
	{
		private var tableId:UInt64;
		
		private var dealerList:Array;

		public function DealerViewManager(tableId:UInt64,tableView:MovieClip)
		{
			super();
			this.tableId = tableId;

			this.mainUI = tableView;

			tableView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			tableView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);

			dealerList = [null, tableView.dealer_1, tableView.dealer_2, tableView.dealer_3, tableView.dealer_4, tableView.dealer_5, tableView.dealer_6];
		}

		/**
		 * 桌子数据
		 **/
		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		private function onAddToStage(event:Event):void
		{
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);

			dispatcher.addEventListener(MSProto.MSID_BANKER_CHAIR_ID_NTF.toString(), onUpdateTableView);
		}

		private function removeFromStage(event:Event):void
		{
			dispatcher.removeEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);
		}

		private function onUpdateTableView(event:MajiangEvent):void
		{
			if (tableId.toString() != event.data.tableId.toString())
				return;
			
			var chairId:int;
			var tablePlayerDataModel:TablePlayerDataModel;
			for (var i:int = 1; i <= 6; i++)
			{
				var position:int = i;
				chairId = tableDataModel.getChairIdByPosition(position);
				tablePlayerDataModel = tableDataModel.seatedPlayers[chairId];

				if (tableDataModel.bankerId == chairId && tablePlayerDataModel != null)
				{
					dealerList[position].visible = true;
				}
				else
				{
					dealerList[position].visible = false;
				}
			}
		}

	}
}
